#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <muParser.h>
#include "math.h"
#include <u.h>
#include <QMessageBox>
#include <rungekutta.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QLayout* pLayout1 = new QBoxLayout(QBoxLayout::TopToBottom, ui->groupBox1);
    ui->groupBox1->setLayout(pLayout1);

    this->pCanvasXT = new MyCanvasXT(ui->groupBox1);
    pLayout1->addWidget(this->pCanvasXT);
    //

    QLayout* pLayout2 = new QBoxLayout(QBoxLayout::TopToBottom, ui->groupBox2);
    ui->groupBox2->setLayout(pLayout2);

    this->pCanvasYT = new MyCanvasYT(ui->groupBox2);
    pLayout2->addWidget(this->pCanvasYT);
    //

    QLayout* pLayout3 = new QBoxLayout(QBoxLayout::TopToBottom, ui->groupBox3);
    ui->groupBox3->setLayout(pLayout3);

    this->pCanvasYX = new MyCanvasYX(ui->groupBox3);
    pLayout3->addWidget(this->pCanvasYX);

    //
    ui->txtAlto->setText("8");
    ui->txtAncho->setText("8");
    ui->txtfuncx->setText("y");
    ui->txtfuncy->setText("-x");
    ui->txtx0->setText("2");
    ui->txty0->setText("2");
    ui->txtN->setText("200");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    try
    {
        this->pCanvasXT->pinto = true;
        this->pCanvasYT->pinto = true;
        this->pCanvasYX->pinto = true;
        this->recargarPropiedades();

    }catch(...)
    {
        qDebug("Error");
    }
}

void MainWindow::recargarPropiedades()
{
    char p[200];
    mu::Parser parser;
    parser.SetExpr(ui->txtAncho->text().toStdString());
    double t0 = 0;
    double tf = parser.Eval();
    parser.DefineFun("u",&U::u_impl,false);
    if(this->pCanvasXT->funcion!=NULL)
    {
        delete this->pCanvasXT->funcion;
        this->pCanvasYT->funcion=NULL;
        this->pCanvasYX->funcion=NULL;
    }
    if(this->ui->rdbRngKta2->isCheckable())
    {
        this->pCanvasXT->funcion=new RungeKutta(2,ui->txtfuncx->text(), ui->txtfuncy->text(),ui->txtx0->text().toDouble(), ui->txty0->text().toDouble(), t0, tf, ui->txtN->text().toDouble());
    }else
    {
        if(this->ui->rdbRngKta4->isCheckable())
        {
            this->pCanvasXT->funcion=new RungeKutta(4,ui->txtfuncx->text(), ui->txtfuncy->text(),ui->txtx0->text().toDouble(), ui->txty0->text().toDouble(),  t0, tf, ui->txtN->text().toDouble());
        }
    }
    this->pCanvasYT->funcion=this->pCanvasXT->funcion;
    this->pCanvasYX->funcion=this->pCanvasXT->funcion;

    this->pCanvasXT->x_cotainferior=this->pCanvasXT->funcion->puntos->minimoT;
    this->pCanvasXT->x_cotasuperior=this->pCanvasXT->funcion->puntos->maximoT;
    this->pCanvasXT->y_cotainferior=this->pCanvasXT->funcion->puntos->minimoX;
    this->pCanvasXT->y_cotasuperior=this->pCanvasXT->funcion->puntos->maximoX;

    this->pCanvasYT->x_cotainferior=this->pCanvasXT->funcion->puntos->minimoT;
    this->pCanvasYT->x_cotasuperior=this->pCanvasXT->funcion->puntos->maximoT;
    this->pCanvasYT->y_cotainferior=this->pCanvasXT->funcion->puntos->minimoY;
    this->pCanvasYT->y_cotasuperior=this->pCanvasXT->funcion->puntos->maximoY;

    this->pCanvasYX->x_cotainferior=this->pCanvasXT->funcion->puntos->minimoX;
    this->pCanvasYX->x_cotasuperior=this->pCanvasXT->funcion->puntos->maximoX;
    this->pCanvasYX->y_cotainferior=this->pCanvasXT->funcion->puntos->minimoY;
    this->pCanvasYX->y_cotasuperior=this->pCanvasXT->funcion->puntos->maximoY;
    this->update();
}




void MainWindow::on_pushButton_2_clicked()
{
    this->pCanvasXT->y_cotainferior=-ui->txtAlto->text().toDouble()/2;
    this->pCanvasXT->y_cotasuperior=ui->txtAlto->text().toDouble()/2;
    this->pCanvasXT->update();
    this->pCanvasYT->y_cotainferior=-ui->txtAlto->text().toDouble()/2;
    this->pCanvasYT->y_cotasuperior=ui->txtAlto->text().toDouble()/2;
    this->pCanvasYT->update();
    this->pCanvasYX->y_cotainferior=-ui->txtAlto->text().toDouble()/2;
    this->pCanvasYX->y_cotasuperior=ui->txtAlto->text().toDouble()/2;
    this->pCanvasYX->update();
}
